// myself/file/DocumentSigning.js
import notification from '../../utils/notification_center.js';
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    flowbill: null,
    showactive: false,
    nextShow: false,
    nextShow1: true,
    flowlog: null,
    finish: true,
    filename: null,
    filePath: null,
    fileId: null,
    signPath: '',
    nextnameid: '',
    nextname: '',
    mid: 0,
    items: null,
    uid: 0,
    modenum:'miniofficia',
    actives: [{
        name: '1',
        value: '同意',
        checked: true
      },
      {
        name: '2',
        value: '不同意',
        checked: false
      },
    ],

    setQm: 0,
    isqm: false,
    nexuids: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mid: options.mid,
      uid: app.globalData.userInfo.uid
    })
    network.postJson(this, api.officialview, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      mid: options.mid,
      modelname: options.modelname
    }).then(res => {
      var isshowactive = false
      if (res.data.data.flowbill.nowcheckid) {
        var nowcheckid = res.data.data.flowbill.nowcheckid.split(",")
        var index = nowcheckid.indexOf(this.data.uid)
        if (index != -1) {
          isshowactive = true
        }
      }
      let showQm = false
      if (res.data.data.isqm == 1 || res.data.data.isqm == 2) {
        showQm = true;
      }
      this.setData({
        flowbill: res.data.data.flowbill,
        items: res.data.data.flowlog,
        showactive: isshowactive,
        nextShow: res.data.data.nextShow,
        setQm: res.data.data.isqm,
        isqm: showQm,
        nexuids: res.data.data.nexuids
      })
      switch (this.data.flowbill.type) {
        case '0':
          this.setData({
            modenum:'miniofficia'
          })
          break
        case '1':
          this.setData({
            modenum:'miniofficib'
          })
          break
        case '2':
          this.setData({
            modenum:'miniofficid'
          })
          break
      }
   
    })
    this.signature = this.selectComponent("#signature");
    this.toast = this.selectComponent("#toast");
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    notification.removeNotification("GetSignature", this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  upload() {
    let that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles
        let url = app.globalData.debug ? api.debugUrl : api.url
        wx.uploadFile({
          url: url + api.upFile, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].path,
          name: 'file',
          formData: {
            adminid: app.globalData.userInfo.uid,
            token: app.globalData.userInfo.token,
            cfrom: "weixin",
            device: app.globalData.device
          },
          success(res) {
            const data = res.data
            console.log(JSON.parse(res.data).id)
            that.data.fileId = JSON.parse(res.data).id
            that.setData({
              filename: JSON.parse(res.data).filename
            })
          }
        })
      }
    })
  },
  downloadFile: function (e) {
    wx.showLoading({title: '加载中…'})
    var type = e.target.dataset.type;
    var filepath = e.target.dataset.url
    if (type == 'jpg' || type == 'jpeg' || type == 'png' || type == 'gif' || type == 'bmp') {
      var imgList = [filepath]; //获取data-list
      wx.previewImage({
        current: filepath, // 当前显示图片的http链接  
        urls: imgList, // 需要预览的图片http链接列表
        success: function (res) {
          wx.hideLoading()
        }  
      })
    } else if (type == 'doc' || type == 'docx' || type == 'xls' || type == 'xlsx' || type == 'ppt' || type == 'pptx' || type == 'pdf') {
      wx.downloadFile({
        url: filepath,
        header: {},
        success: function (res) {
          var filePath = res.tempFilePath;
          console.log(filePath);
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
              wx.hideLoading()
            },
            fail: function (res) {
              console.log(res);
            },
            complete: function (res) {
              console.log(res);
            }
          })
        },
        fail: function (res) {
          console.log('文件下载失败');
        },
        complete: function (res) {},
      })

    }
  },

  goSignature() {
    this.signature.openSignature();
  },
  // bindNextChange1: function (e) {
  //   let applyIndex = e.detail.value
  //   let nextUserList = this.data.nextUsers
  //   let dataNextUser = this.data.dataNextUser
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     applyIndex:applyIndex,
  //     nextnameid:nextUserList[applyIndex].id,
  //     nextname:dataNextUser[applyIndex].name
  //   })
  // },
  bindNextChange: function (e) {
    let nexuids = e.currentTarget.dataset.nexuids
    wx.navigateTo({
      url: '/myself/file/ChooseUser?nexuids=' + nexuids + '&selectid=' + this.data.nextnameid + '&nextname=' + this.data.nextname
    })
  },
  radioChange: function (e) {
    let value = e.detail.value
    if (value == 1) {
      this.setData({
        nextShow1: true
      })
    } else {
      this.setData({
        nextShow1: false
      })
    }
    let isQm = false;
    if (this.data.setQm == 1) {
      isQm = true
    } else {
      if (this.data.setQm == 2 && value == 1) {
        isQm = true
      } else if (this.data.setQm == 3 && value == 2) {
        isQm = true
      }
    }
    this.setData({
      isqm: isQm
    })
  },

  getSignature(src) {
    this.setData({
      path: src.detail
    })
    let that = this;
    let url = app.globalData.debug ? api.debugUrl : api.url
    wx.uploadFile({
      url: url + api.upFile, //仅为示例，非真实的接口地址
      filePath: this.data.path,
      name: 'file',
      formData: {
        adminid: app.globalData.userInfo.uid,
        token: app.globalData.userInfo.token,
        cfrom: "weixin",
        device: app.globalData.device
      },
      success(res) {
        const data = res.data
        that.data.signPath = JSON.parse(res.data).filepath
      }
    })
  },

  /**
   * 请求数据
   * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
   */

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (this.data.isqm && !this.data.signPath) {
      this.toast.showToast('请上传签名');
      return;
    }
    if (e.detail.value.isAgree == 2) {
      if (!e.detail.value.explain) {
        this.toast.showToast('请写明不同意原因');
        return;
      }
    }
    network.postJson(this, api.opt, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      zt: e.detail.value.isAgree,
      sm: e.detail.value.explain,
      tuiid: this.data.flowbill.tuiid,
      fileid: '',
      modenum: this.data.modenum,
      mid: this.data.mid,
      qmimgstr: this.data.signPath,
      nextnameid: this.data.nextnameid,
      nextname: this.data.nextname
    }).then(res => {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        success:function(){
          setTimeout(function () {
            //要延时执行的代码
            wx.navigateBack();
          }, 2000) //延迟时间
        }
      })
    });
  }
})